<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="showAddCateDialog"
            >添加分类</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格 -->
      <tree-table
        :data="catelist"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        :show-index="true"
        border
        text-index="#"
      >
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted === false"
            style="color: lightgreen"
          ></i>
          <i class="el-icon-error" style="color: red" v-else></i>
        </template>

        <!-- 这是排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0" size="mini">一级</el-tag>
          <el-tag
            v-else-if="scope.row.cat_level === 1"
            size="mini"
            type="success"
            >二级</el-tag
          >
          <el-tag v-else size="mini" type="warning">三级级</el-tag>
        </template>

        <template slot="opt" slot-scope="scope">
          <el-button size="mini" type="primary" @click="showEditCatedialog(scope.row)" icon="el-icon-edit"
            >编辑</el-button
          >
          <el-button size="mini" type="danger" @click="removeCate(scope.row.cat_id)" icon="el-icon-delete"
            >删除</el-button
          >
        </template>
      </tree-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryinfo.pagenum"
        :page-sizes="[1, 4, 6, 10, 15]"
        :page-size="queryinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加分类对话框 -->
    <el-dialog
      width="50%"
      title="添加分类"
      :visible.sync="addCatedialogVisable"
      @close="addCatedialogClose"
    >
      <el-form :model="addCateFrom" :rules="addCateFromRules" ref="addCateRef" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateFrom.cat_name"></el-input>
        </el-form-item>

        <el-form-item label="父级分类">
            <div>
          <el-cascader  expand-trigger="hover"  :options="parentCatelist" :props="cateProps"
            v-model="selectedKyes"
            @change="parentCatechange"
            clearable
            change-on-select
          ></el-cascader>
            </div>
         
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addCatedialogVisable = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>

   <!-- 修改分类名 -->
    <el-dialog
      width="50%"
      title="添加分类"
      :visible.sync="editCatedialogVisable"
      @close="editCatedialogClose"
    >
      <el-form :model="editCateFrom" :rules="editCateFromRules" ref="editCateRef" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="editCateFrom.cat_name"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editCatedialogVisable = false">取 消</el-button>
        <el-button type="primary" @click="editCate">确 定</el-button>
      </span>
    </el-dialog>
    
  </div>
</template>
<script>
export default {
  data() {
    return {
      queryinfo: {
        type: 3,
        pagenum: 1,
        pagesize: 10,
      },
      catelist: [],
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name',
        },
        {
          label: '是否有效',
          prop: 'cat_deleted',
          type: 'template',
          template: 'isok',
        },
        {
          label: '排序',
          type: 'template',
          template: 'order',
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt',
        },
      ],
      addCatedialogVisable: false,
      addCateFrom: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0,
      },
      addCateFromRules: {
        cat_name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
        ],
      },
      parentCatelist:[],
      cateProps:{
          value:'cat_id',
          label:'cat_name',
          children:'children'
      },
      selectedKyes:[],
      editCateFrom:{
          cat_name: '',
          cat_id:''
      },
       editCateFromRules: {
        cat_name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
        ]
      },
      editCatedialogVisable:false,
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryinfo,
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品分类失败！')
      }
      console.log(res)
      this.catelist = res.data.result
      this.total = res.data.total
    },
    handleSizeChange(newSize) {
      this.queryinfo.pagesize = newSize
      this.getCateList()
    },
    handleCurrentChange(newpage) {
      this.queryinfo.pagenum = newpage
      this.getCateList()
    },
    showAddCateDialog() {
      this.getParentCateList()
      this.addCatedialogVisable = true
      
    },
   async getParentCateList(){
        const {data:res} = await this.$http.get('categories',{params:{type:2}})
        if(res.meta.status!==200){
            return this.$message.error('请求类别错误')
        }
        this.parentCatelist=res.data
        
    },
    parentCatechange(){
        console.log(this.selectedKyes)
        if(this.selectedKyes.length>0){
           this.addCateFrom.cat_pid= this.selectedKyes[this.selectedKyes.length-1]
           this.addCateFrom.cat_level=this.selectedKyes.length
           return
        }{
            this.addCateFrom.cat_pid=0
            this.addCateFrom.cat_level=0
        }
    },
    addCate(){
         this.$refs.addCateRef.validate(async (valid) => {
        if (!valid) return
        const { data: res } = await this.$http.post('categories', this.addCateFrom)
        if (res.meta.status !== 201) {
          this.$message.error('添加类别失败！')
        }
        this.$message.success('添加类别成功！')
        this.getCateList()
        this.addCatedialogVisable = false
        
      })
    },
    addCatedialogClose(){
        this.$refs.addCateRef.resetFields()
        this.selectedKyes=[]
        this.addCateFrom.cat_pid=0
        this.addCateFrom.cat_level=0
    },
    editCatedialogClose(){
        this.$refs.editCateRef.resetFields()
    },
    showEditCatedialog(row){
        
        this.editCatedialogVisable=true
       this.editCateFrom.cat_name=row.cat_name
       this.editCateFrom.cat_id=row.cat_id
    },
    editCate(){
     this.$refs.editCateRef.validate(async (valid) => {
        if (!valid) return
        const { data: res } = await this.$http.put(
          'categories/' + this.editCateFrom.cat_id,
          {
            cat_name: this.editCateFrom.cat_name
            
          }
        )
        
        if (res.meta.status !== 200) {
          this.$message.error('修改分类名称失败！')
        }
        this.$message.success('修改分类名称成功！')
        this.editCatedialogVisable = false
        this.getCateList()
      })
    },
   async removeCate(id){
     const confirmResult = await this.$confirm(
        '此操作将删除商品分类, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已经取消删除！')
      }
      const { data: res } = await this.$http.delete('categories/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('删除分类失败！')
      }
      this.$message.success('删除分类成功！')
      this.getCateList()
    }

  },
}
</script>
<style  scoped>
.zk-table {
  margin-top: 15px;
  margin-bottom: 15px;
}
.el-cascader{
    width: 100%;
}
</style>